class Solution {
public:
    string simplifyPath(string path) {
        vector<string> stk;
        istringstream ss(path);
        string s;
        while (getline(ss, s, '/'))
        {
            if (s.empty() || s == ".")
                continue;
            if (s != "..")
                stk.push_back(s);
            else if (!stk.empty())
                stk.pop_back();
        }

        string ans;
        for (string& s : stk)
        {
            ans += '/';
            ans += s;
        }
        return stk.empty() ? "/" : ans;
    }
};